//
//  SelectCityViewController.m
//  PersonalProjects
//
//  Created by lanou3g on 15/9/11.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "SelectCityViewController.h"
#import "YPKStatisticsRequest.h"

@interface SelectCityViewController ()

@end

@implementation SelectCityViewController

@synthesize delegate = _delegate;

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _citysArray = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //获取热门城市的网络请求
    [self getHotCityListRequest];
}

#pragma mark - 发送请求
- (void)getHotCityListRequest {
    [YPKStatisticsRequest getHotCityListSuccess:^(id responseObject) {
        DLog(@"成功");
        
        NSDictionary *dic = responseObject[@"result"];
        _citysArray = dic.allValues;
        [_mainTableView reloadData];
        
    } failure:^(NSError *error) {
        DLog(@"失败");
    }];
}

#pragma mark - TableView Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _citysArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HotCityTableViewCell" forIndexPath:indexPath];
    self.cityLabel.text = _citysArray[indexPath.row][@"name"];
    return cell;
}

#pragma mark - TableView Delegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 23)];
    view.backgroundColor = [Untility colorFromHexRGB:@"F5F5F5"];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(14, 2, 60, 21)];
    lable.text = @"选择城市";
    lable.font = [UIFont boldSystemFontOfSize:15];
    lable.backgroundColor = [UIColor clearColor];
    
    [view addSubview:lable];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 23;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([_delegate respondsToSelector:@selector(SelectCityViewControllerChangeCity:city_id:)]) {
        [_delegate SelectCityViewControllerChangeCity:_citysArray[indexPath.row]
                                              city_id:_citysArray[indexPath.row][@"city_id"]];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
